package tab

import (
	"fmt"
	"net/http"

	"code.google.com/awwwe-browser/auralvm/conversation"
	"code.google.com/awwwe-browser/auralvm/param"
	"code.google.com/awwwe-browser/auralvm/setting"
	"code.google.com/awwwe-browser/data"
)

// HandlerTabListTools handles an http request to list the tab tools for the view.
func HandlerTabListTools(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	ContinuerTabListTools(w, r, data.EmptyString, -1, true)
}

// ContinuerTabListTools continues listing of tab tools.
func ContinuerTabListTools(w http.ResponseWriter, r *http.Request, intro string, startoptionsat int, continueon bool) {
	var err error
	var conv *conversation.Conversation
	var option conversation.APIOption
	var jsonbb []byte
	if BrowserTabs.Len() == 0 {
		intro = "All of your tabs are closed."
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			StopContinuer(w, r, intro)
		}
		return
	}
	conv = conversation.NewConversation()
	conv.Type = conversation.ConversationTypeOptions
	conv.Intro = intro
	conv.InitialIndex = startoptionsat
	conv.Header.Append("These are the tab tools.")
	if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
		// a lot of help or a little help.
		// add helpful
		conv.Helpful = " I will iterate through the names of each of the tools. After i say the name of a tool, say yes, if you want to use that tool."
	}
	if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
		// a lot of help
		// add more helpful
		conv.MoreHelpful = " You may say, yes, back, restart, finish, help, pause, or quit."
	}
	conv.Footer = "That is the end of the list of tab tools."
	conv.CallHelp.Package = param.Package
	conv.CallHelp.Object = "browser"
	conv.CallHelp.Method = "listtools"
	// listen tool
	option = conversation.BuildAPIOption()
	option.VXMLs.Append("Listen to a web page which is open in a tab.")
	option.CallYes.Package = param.Package
	option.CallYes.Object = "tab"
	option.CallYes.Method = "listtolisten"
	option.CallYes.Arguments[param.ParamSourceObject] = "tab"
	option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
	option.CallYes.Arguments[param.ParamSourceOptionIndex] = "1"
	conv.Options = append(conv.Options, option)
	// bookmark tool
	option = conversation.BuildAPIOption()
	option.VXMLs.Append("Book mark a web page which is open in a tab.")
	option.CallYes.Package = param.Package
	option.CallYes.Object = "tab"
	option.CallYes.Method = "listtobookmark"
	option.CallYes.Arguments[param.ParamSourceObject] = "tab"
	option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
	option.CallYes.Arguments[param.ParamSourceOptionIndex] = "2"
	conv.Options = append(conv.Options, option)
	// close tool
	option = conversation.BuildAPIOption()
	option.VXMLs.Append("Close a tab.")
	option.CallYes.Package = param.Package
	option.CallYes.Object = "tab"
	option.CallYes.Method = "listtoclose"
	option.CallYes.Arguments[param.ParamSourceObject] = "tab"
	option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
	option.CallYes.Arguments[param.ParamSourceOptionIndex] = "0"
	conv.Options = append(conv.Options, option)
	// serialize the conversation
    jsonbb, err = conv.Marshal()
	if err == nil {
		w.Write(jsonbb)
	} else {
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			StopContinuer(w, r, intro)
		}
	}
}

// HandlerTabListToClose handles an http request to list open tabs for closing for the view.
func HandlerTabListToClose(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	ContinuerTabListToClose(w, r, data.EmptyString, -1, true)
}

// ContinuerTabListToClose continues with listing of open tabs for closing.
func ContinuerTabListToClose(w http.ResponseWriter, r *http.Request, intro string, startoptionsat int, continueon bool) {
	var err error
	var conv *conversation.Conversation
	var option conversation.APIOption
	var tab *Tab
	var i, length, last int
	var jsonbb []byte
	if BrowserTabs.Len() == 0 {
		intro = "All of your tabs are closed."
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			// internal server error
			StopContinuer(w, r, intro)
		}
		return
	}
	conv = conversation.NewConversation()
	conv.Type = conversation.ConversationTypeOptions
	conv.Intro = intro
	conv.InitialIndex = startoptionsat
	conv.Header.Append("This tool lets you close tab.")
	if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
		// a lot of help or a little help.
		// add helpful
		conv.Helpful = "I will iterate through the names of every web page which you have open in a tab."
	}
	if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
		// a lot of help
		// add more helpful
		conv.MoreHelpful = "After i say the name of a web page, say yes, if you want to close the tab."
	}
	conv.Footer = "That is the end of the list of tabs."
	conv.CallHelp.Package = param.Package
	conv.CallHelp.Object = "tab"
	conv.CallHelp.Method = "listtools"
	length = BrowserTabs.Len()
	last = length - 1
	for i = 0; i < length; i++ {
		tab = BrowserTabs.AtIndex(i)
		option = conversation.BuildAPIOption()
		option.VXMLs.Append(tab.Title)
		option.CallYes.Package = param.Package
		option.CallYes.Object = "tab"
		option.CallYes.Method = "close"
		option.CallYes.Arguments[param.ParamTabKey] = fmt.Sprintf("%d", i)
		if i < last {
			// if a tab is not able to open then the vm should continue with listing tabs to open.
			option.CallYes.Arguments[param.ParamSourceObject] = "tab"
			option.CallYes.Arguments[param.ParamSourceMethod] = "listtoclose"
			option.CallYes.Arguments[param.ParamSourceOptionIndex] = fmt.Sprintf("%d", i)
		} else {
			// if a tab is not able to open then the vm should continue with listing the list of tab tools.
			option.CallYes.Arguments[param.ParamSourceObject] = "tab"
			option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
			option.CallYes.Arguments[param.ParamSourceOptionIndex] = "0"
		}
		conv.Options = append(conv.Options, option)
	}
	// serialize the conversation
	jsonbb, err = conv.Marshal()
	if err == nil {
		w.Write(jsonbb)
	} else {
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			StopContinuer(w, r, intro)
		}
	}
}

// HandlerTabListToListen handles an http request to list the current tabs to listen to one for the view.
func HandlerTabListToListen(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	ContinuerTabListToListen(w, r, data.EmptyString, -1, true)
}

// ContinuerTabListToListen continues with the listing of current tabs to listen to one.
func ContinuerTabListToListen(w http.ResponseWriter, r *http.Request, intro string, startoptionsat int, continueon bool) {
	var err error
	var conv *conversation.Conversation
	var option conversation.APIOption
	var tab *Tab
	var i, length, last int
	var jsonbb []byte
	if BrowserTabs.Len() == 0 {
		intro = "All of your tabs are closed."
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			// internal server error
			StopContinuer(w, r, intro)
		}
		return
	}
	conv = conversation.NewConversation()
	conv.Type = conversation.ConversationTypeOptions
	conv.Intro = intro
	conv.InitialIndex = startoptionsat
	conv.Header.Append("This tool lets you listen to a web page which is open in a tab.")
	if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
		// a lot of help or a little help.
		// add helpful
		conv.Helpful = "I will iterate through the names of every web page which you have open in a tab."
	}
	if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
		// a lot of help
		// add more helpful
		conv.MoreHelpful = "After i say the name of a web page, say yes, if you want to listen to the web page."
	}
	conv.Footer = "That is the end of the list of tabs."
	conv.CallHelp.Package = param.Package
	conv.CallHelp.Object = "tab"
	conv.CallHelp.Method = "listtools"
	length = BrowserTabs.Len()
	last = length - 1
	for i = 0; i < length; i++ {
		tab = BrowserTabs.AtIndex(i)
		option = conversation.BuildAPIOption()
		option.VXMLs.Append(tab.Title)
		option.CallYes.Package = param.Package
		option.CallYes.Object = "tab"
		option.CallYes.Method = "listen"
		option.CallYes.Arguments[param.ParamTabKey] = fmt.Sprintf("%d", tab.Key)
		if i < last {
			// if a tab is not able to be spoken then the vm should continue with listing tabs to open.
			option.CallYes.Arguments[param.ParamSourceObject] = "tab"
			option.CallYes.Arguments[param.ParamSourceMethod] = "listtolisten"
			option.CallYes.Arguments[param.ParamSourceOptionIndex] = fmt.Sprintf("%d", i)
		} else {
			// if a tab is not able to be spoken then the vm should continue with listing the list of tab tools.
			option.CallYes.Arguments[param.ParamSourceObject] = "tab"
			option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
			option.CallYes.Arguments[param.ParamSourceOptionIndex] = "0"
		}
		conv.Options = append(conv.Options, option)
	}
	// serialize the conversation
	jsonbb, err = conv.Marshal()
	if err == nil {
		w.Write(jsonbb)
	} else {
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			StopContinuer(w, r, intro)
		}
	}
}

// HandlerTabListToBookmark handles an http request to list the current tabs to book mark one for the view.
func HandlerTabListToBookmark(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	ContinuerTabListToBookmark(w, r, data.EmptyString, -1, true)
}

// ContinuerTabListToBookmark continues with the listing of current tabs to book mark one.
func ContinuerTabListToBookmark(w http.ResponseWriter, r *http.Request, intro string, startoptionsat int, continueon bool) {
	var err error
	var conv *conversation.Conversation
	var option conversation.APIOption
	var tab *Tab
	var i, length, last int
	var jsonbb []byte
	if BrowserTabs.Len() == 0 {
		intro = "All of your tabs are closed."
		NextContinuer(w, r, intro)
		return
	}
	conv = conversation.NewConversation()
	conv.Type = conversation.ConversationTypeOptions
	conv.Intro = intro
	conv.InitialIndex = startoptionsat
	conv.Header.Append("This tool lets you book mark a web page which is open in a tab.")
	if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
		// a lot of help or a little help.
		// add helpful
		conv.Helpful = "I will iterate through the names of every web page which you have open in a tab."
	}
	if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
		// a lot of help
		// add more helpful
		conv.MoreHelpful = "After i say the name of a web page, say yes, if you want to book mark that web page."
	}
	conv.Footer = "That is the end of the list of tabs."
	conv.CallHelp.Package = param.Package
	conv.CallHelp.Object = "tab"
	conv.CallHelp.Method = "listtools"
	length = BrowserTabs.Len()
	last = length - 1
	for i = 0; i < length; i++ {
		tab = BrowserTabs.AtIndex(i)
		option = conversation.BuildAPIOption()
		option.VXMLs.Append(tab.Title)
		option.CallYes.Package = param.Package
		option.CallYes.Object = "bookmark"
		option.CallYes.Method = "add"
		option.CallYes.Arguments[param.ParamBookMarkTitle] = tab.Title
		option.CallYes.Arguments[param.ParamBookMarkURL] = tab.URL
		if i < last {
			// if a tab is not able to be spoken then the vm should continue with listing tabs to open.
			option.CallYes.Arguments[param.ParamSourceObject] = "tab"
			option.CallYes.Arguments[param.ParamSourceMethod] = "listtobookmark"
			option.CallYes.Arguments[param.ParamSourceOptionIndex] = fmt.Sprintf("%d", i)
		} else {
			// if a tab is not able to be spoken then the vm should continue with listing the list of tab tools.
			option.CallYes.Arguments[param.ParamSourceObject] = "tab"
			option.CallYes.Arguments[param.ParamSourceMethod] = "listtools"
			option.CallYes.Arguments[param.ParamSourceOptionIndex] = "0"
		}
		conv.Options = append(conv.Options, option)
	}
	// serialize the conversation
	jsonbb, err = conv.Marshal()
	if err == nil {
		w.Write(jsonbb)
	} else {
		if continueon {
			NextContinuer(w, r, intro)
		} else {
			StopContinuer(w, r, intro)
		}
	}
}
